using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using JNPF.Basics.Models;
using JNPF.Basics.Logic;
using JNPF.Utils;
using System.Threading.Tasks;
using System.Linq;
using JNPF.Utils.Controller;
using JNPF.Basics.Logic.Common.Login;
using JNPF.Utils.Extensions;
using Castle.Core.Resource;

namespace JNPF.Basics.Controller
{
    [Route("api/Jiushi/[Controller]")]
    public class CustomerneedController : BaseApiController
    {
        private CustomerneedBll CustomerneedService = new CustomerneedBll();
        private CustomerbaseBll CustomerbaseService = new CustomerbaseBll();
        private CustomeruserBll CustomeruserService = new CustomeruserBll();

        private BillRuleBll billRuleService = new BillRuleBll();
        private DataInterfaceBll dataInterfaceBll = new DataInterfaceBll();
        private UserBll userBll = new UserBll();
        private OrganizeBll organizeBll = new OrganizeBll();


        #region GET

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(int id)
        {
            var data = await CustomerneedService.GetInfo(id);
            var output = data.MapTo<CustomerneedUpOutput>();
            //取出管家和企业名称信息
            var customerinfo = await CustomerbaseService.GetInfo(data.CustomerId);
            if (customerinfo != null)
            {

                output.customerName = customerinfo.CustomerName;
                output.cutomerBaseType = customerinfo.Type;
            }

            CustomeruserEntity customerUser = await CustomeruserService.GetInfo(data.CustomerId, 1);
            if (customerUser != null)
            {
                var userInfo = await userBll.GetInfo(customerUser.UserId);
                if (userInfo != null)
                {
                    output.customerUserName = userInfo.RealName;

                    output.customerUserPhone = userInfo.MobilePhone;

                    if (customerinfo.Type.HasValue && customerinfo.Type == 3)
                    {
                        output.customerUserPhone = customerinfo.MobilePhone;
                    }
                }
            }

            return Success(output);
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetListByUserId/{id}")]
        public async Task<ResponseResult> GetListByUserId(CustomerneedListQueryInput input)
        {
            var data = await CustomerneedService.GetPageList(input);
            var outputList = data.list.MapToList<CustomerneedListOutput>();
            outputList.ForEach(o =>
            {
                o.needDate = o.needDate != null ? string.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(o.needDate)) : null;
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }




        /// <summary>
        /// 企业用户  ===  手机号码对应起来的 
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetBusPageList/{id}")]
        public async Task<ResponseResult> GetBusPageList(CustomerneedListQueryInput input)
        {
            var data = await CustomerneedService.GetBusPageList(input);

            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 客户经理列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetCuseBusList/{id}")]
        public async Task<ResponseResult> GetCuseBusList(CustomerneedListQueryInput input)
        {
            var data = await CustomerneedService.GetCuseBusList(input);

            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }




        /// <summary>
        /// 信贷管家（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetPagCuseList/{id}")]
        public async Task<ResponseResult> GetPagCuseList(CustomerneedListQueryInput input)
        {
            var data = await CustomerneedService.GetPagCuseList(input);

            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }



        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(CustomerneedListQueryInput input)
        {
            var data = await CustomerneedService.GetPageList(input);
            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }


        #endregion

        #region POST

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">新建输入</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] CustomerneedCrInput input)
        {
            var entity = input.MapTo<CustomerneedEntity>();
            await CustomerneedService.Create(entity);
            return Success("新建成功");
        }



        /// <summary>
        /// 结束
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns> 
        [HttpPut("End/{id}")]
        public async Task<ResponseResult> EndLending(int id)
        {
            var entity = await CustomerneedService.GetInfo(id);

            entity.Type = 2;

            await CustomerneedService.Update(id, entity);
            return Success("结束成功");
        }




        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键id</param>
        /// <param name="input">编辑输入</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(int id, [FromBody] CustomerneedCrInput input)
        {
            var entity = input.MapTo<CustomerneedEntity>();
            var isOk = await CustomerneedService.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(int id)
        {
            var entity = await CustomerneedService.GetInfo(id);
            var isOk = await CustomerneedService.Delete(entity);
            return isOk > 0 ? Success("删除成功") : Error("删除失败");
        }

        #endregion

    }
}